package com.book.lizhi.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.book.lizhi.AppApplication;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.model.BookshelfModel;

public class BookshelfAdaper extends AppAdapter {

	public BookshelfAdaper(Context context, List<?> list) {
		super(context, list);
	}

	@Override
	public int getItemViewType(int position) {
		return position == getCount() - 1 ? 1 : 0;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(final int position, View convertView, final Context context) {
		int itemType = getItemViewType(position);
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.bookshelf_item_layout, null);
			holder = new ViewHolder();
			holder.coverView = (ImageView) convertView.findViewById(R.id.book_cover_view);
			holder.nameView = (TextView) convertView.findViewById(R.id.book_name_view);
			holder.rateView = (TextView) convertView.findViewById(R.id.book_rate_view);
			holder.checkContainer = convertView.findViewById(R.id.bookshelf_check_layout);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.bookshelf_checkBox);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (itemType == 0) {
			final BookshelfModel model = (BookshelfModel) getItem(position);
			ImageListener listener = ImageLoader.getImageListener(holder.coverView, R.drawable.cover_default, R.drawable.cover_default);
			AppApplication.instance.imageLoader.get(model.img, listener);
			holder.nameView.setText(model.name);
			holder.nameView.setVisibility(View.VISIBLE);
			holder.rateView.setVisibility(View.VISIBLE);
			holder.rateView.setText(model.rPercent);
			if (model.isCheckModel) {
				holder.checkContainer.setVisibility(View.VISIBLE);
				holder.checkBox.setChecked(model.isCheck);
				holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						Intent intent = new Intent(Constants.BOOKSHELF_CHECK_ACTION);
						intent.putExtra(Constants.CHECK_POSITION, position);
						intent.putExtra(Constants.IS_CHECK, isChecked);
						LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
						if (isChecked) {
							model.isCheck = true;
						} else {
							model.isCheck = false;
						}
					}
				});
			} else {
				holder.checkContainer.setVisibility(View.GONE);
				holder.checkBox.setOnCheckedChangeListener(null);
			}
		} else {
			holder.coverView.setImageResource(R.drawable.bookshelf_default);
			holder.nameView.setVisibility(View.GONE);
			holder.rateView.setVisibility(View.GONE);
			holder.checkContainer.setVisibility(View.GONE);
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView coverView;
		TextView nameView;
		TextView rateView;
		View checkContainer;
		CheckBox checkBox;
	}

}
